<template>
  <HomePanel title="热门品牌" subTitle="国际经典 品质保证" ref="target">
    <template v-slot:right>
      <a
        href="javascript:"
        @click="handleButton(0)"
        class="iconfont icon-angle-left prev"
        :class="{ active: lightButtonPrev }"
      ></a>
      <a
        href="javascript:"
        @click="handleButton(1)"
        class="iconfont icon-angle-right next"
        :class="{ active: lightButtonNext }"
      ></a>
    </template>
    <template v-slot:default>
      <div class="box">
        <!-- 有数据时 -->
        <ul
          class="list"
          :style="{ transform: `translateX(${-currentIndex * 1240}px)` }"
          v-if="brandData.length"
        >
          <li v-for="item in brandData" :key="item.id">
            <RouterLink to="/">
              <img :src="item.picture" alt="" />
            </RouterLink>
          </li>
        </ul>
        <!-- 没有数据时 -->
        <transition name="fade">
          <div v-if="brandData.length === 0" class="skeleton">
            <xtx-skeleton
              class="item"
              v-for="i in 5"
              :key="i"
              animated="scroll"
              bg="#e4e4e4"
              width="240px"
              height="305px"
            />
          </div>
        </transition>
      </div>
    </template>
  </HomePanel>
</template>

<script>
import HomePanel from "../currency/HomePanel";
import useLazyData from "../../hooks/useLazyData";
import { getBrandData } from "../../api/home";
import { ref } from "vue";

export default {
  name: "HomeBrand",
  components: { HomePanel },
  setup() {
    let { target, result: brandData } = useLazyData(getBrandData(10));
    // 用来计算移动的长度
    let currentIndex = ref(0);
    // 展示哪个按钮高亮
    let lightButtonPrev = ref(false);
    let lightButtonNext = ref(true);

    // 点击按钮
    const handleButton = (num) => {
      currentIndex.value = num;
      if (num === 0) {
        lightButtonPrev.value = false;
        lightButtonNext.value = true;
      } else {
        lightButtonPrev.value = true;
        lightButtonNext.value = false;
      }
    };

    return {
      target,
      brandData,
      currentIndex,
      handleButton,
      lightButtonPrev,
      lightButtonNext,
    };
  },
};
</script>

<style scoped lang="less">
.home-panel {
  background: #f5f5f5;
}

.iconfont {
  width: 20px;
  height: 20px;
  background: #ccc;
  color: #fff;
  display: inline-block;
  text-align: center;
  margin-left: 5px;

  &::before {
    font-size: 12px;
    position: relative;
    top: -2px;
  }

  &.disabled {
    background: #ccc;
    cursor: not-allowed;
  }
}

.active {
  background: @xtxColor;
}

.box {
  display: flex;
  width: 100%;
  height: 345px;
  overflow: hidden;
  padding-bottom: 40px;

  .list {
    width: 200%;
    display: flex;
    transition: all 1s;

    li {
      margin-right: 10px;
      width: 240px;

      &:nth-child(5n) {
        margin-right: 0;
      }

      img {
        width: 240px;
        height: 305px;
      }
    }
  }
}

.skeleton {
  width: 100%;
  display: flex;
  top: 115px;

  .item {
    margin-right: 10px;

    &:nth-child(5n) {
      margin-right: 0;
    }
  }
}
</style>
